// lib/axios.ts
import axios from "axios";

// 创建 axios 请求实例
const instance = axios.create({
    baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:8081", // 基础地址
    timeout: 10000, // 超时时间
    withCredentials: false, // 跨域请求是否需要携带 cookie
});

// 请求拦截器
instance.interceptors.request.use(
    (config) => {
        const token = typeof window !== "undefined" ? localStorage.getItem("token") : null;
        if (token) {
            config.headers.Authorization = token;
        }
        return config;
    },
    (error) => Promise.reject(error)
);

// 响应拦截器
instance.interceptors.response.use(
    response => {
        return response.data;
    },
    (error) => {
        console.error("请求出错:", error);
        return Promise.reject(error);
    }
);

export default instance;
